package monster.com.cvh.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import monster.com.cvh.R;

/**
 * Created by ASUS on 2018/2/28.
 */

public class HomeFragment extends Fragment {
    Unbinder unbinder;
    @BindView(R.id.tv_title_bar)
    TextView tvTitleBar;
    @BindView(R.id.tb_fragment_home)
    SlidingTabLayout tbFragmentHome;
    @BindView(R.id.vp_fragment_home)
    ViewPager vpFragmentHome;
    private View view;
    private String[] mTitles = {"攻略", "干货", "活动"};
    private ArrayList<NewsFragment> mFragments;
    private MyViewPagerAdapter mAdapter;
    private NewsFragment strategyFragment;
    private NewsFragment ganHuoFragment;
    private NewsFragment activityFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initData() {
        mFragments = new ArrayList<>();

            if (strategyFragment == null) {
                strategyFragment = new NewsFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("type", 1);
                strategyFragment.setArguments(bundle);
                mFragments.add(strategyFragment);
            }else {
                Bundle bundle = new Bundle();
                bundle.putInt("type", 1);
                strategyFragment.setArguments(bundle);
                mFragments.add(strategyFragment);
            }


            if (ganHuoFragment == null) {
                ganHuoFragment = new NewsFragment();
                Bundle bundle2 = new Bundle();
                bundle2.putInt("type", 2);
                ganHuoFragment.setArguments(bundle2);
                mFragments.add(ganHuoFragment);
            }else {
                Bundle bundle2 = new Bundle();
                bundle2.putInt("type", 2);
                ganHuoFragment.setArguments(bundle2);
                mFragments.add(ganHuoFragment);
            }


            if (activityFragment == null) {
                activityFragment = new NewsFragment();
                Bundle bundle3 = new Bundle();
                bundle3.putInt("type", 3);
                activityFragment.setArguments(bundle3);
                mFragments.add(activityFragment);
            }else {
                Bundle bundle3 = new Bundle();
                bundle3.putInt("type", 3);
                activityFragment.setArguments(bundle3);
                mFragments.add(activityFragment);
            }




        mAdapter = new MyViewPagerAdapter(getFragmentManager());
        vpFragmentHome.setAdapter(mAdapter);
        tbFragmentHome.setViewPager(vpFragmentHome);
        vpFragmentHome.setOffscreenPageLimit(-1);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private class MyViewPagerAdapter extends FragmentStatePagerAdapter {

        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

    }
}
